package thread_pool;



/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/23
 * Time:15:48
 * 实现线程级别的数据传递
 **/
public class PoolDemo29 {
    public static void main(String[] args) {
        //模拟用户登录
        User user = new User();
        user.setUsername("java");
        ThreadLocalUtil.threadLocal.set(user);

        //调用日志模块
        Logger logger = new Logger();
        logger.addLog();

        //调用订单模块
        Order order = new Order();
        order.getOrder();
    }

    static class ThreadLocalUtil{
        static ThreadLocal<User> threadLocal = new ThreadLocal<>();

    }

    static class User{
        private String username;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }
    }
    //伪代码

    /**
     * 日志系统
     */
    static class Logger{
        public void addLog() {
            //得到用户信息
            User user = ThreadLocalUtil.threadLocal.get();
            //...
            System.out.println("添加日志："+user.getUsername());
        }
    }

    /**
     * 订单系统
     */
    static class Order{
        public void getOrder(){
            User user = ThreadLocalUtil.threadLocal.get();
            //...
            System.out.println("订单列表："+user.getUsername());
        }
    }


}
